﻿package com.flashframeless.gallery.modules.thumbs {
	
	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.utils.Dictionary;
	
	import com.hydrotik.utils.QueueLoader;
	import com.hydrotik.utils.QueueLoaderEvent;
	
	import gs.TweenLite;
	import gs.easing.Quint;
	
	public class ThumbItem extends Sprite {
		
		private var _alvo		: Sprite;
		private var _bg			: Sprite;
		private var _mask		: Sprite;
		private var _bar		: Sprite;
		private var _label		: TextField;
		private var _loader		: QueueLoader;
		public var 	data		: Object;
		
		private var dictTypes	:Dictionary;
		
		public function ThumbItem() {
			
			instanciate();
			
		}
		
		private function instanciate():void {
			
			_alvo 	= alvoMc;
			_bg 	= bgMc;
			_bar	= barMc;
			_mask 	= maskMc;
			_label 	= labelTxt;
			
			dictTypes = new Dictionary();
			dictTypes['big'] 	= { width:190 };
			dictTypes['small'] 	= { width:140 };
			
			setupInteractions();
			
		}
		
		private function setupInteractions():void {
			
			
			
		}
		
		private function onLoadQueueProgress( evt:QueueLoaderEvent ):void {
			
			percentTxt.text = Math.floor( evt.percentage*100 ) + '%';
			
		}
		
		private function onLoadQueueComplete( evt:QueueLoaderEvent ):void {
			
			_loader.removeEventListener( QueueLoaderEvent.QUEUE_COMPLETE, onLoadQueueComplete );
			_loader.removeEventListener( QueueLoaderEvent.QUEUE_PROGRESS, onLoadQueueProgress );
			
			TweenLite.to( _alvo, .3, { alpha:1, ease:Quint.easeOut, onComplete:function() {
				
				percentTxt.visible = false;
				
			} } );
			
		}
		
		public function set label( strLabel:String ):void {
			
			_label.text = strLabel;
			
		}
		
		public function set type( strType:String ):void {
			
			_bg.width = _bar.width = _mask.width = _label.width = dictTypes[strType].width;
			
		}
		
		public function set image( imageUrl:String ):void {
			
			_alvo.alpha = 0;
			
			_loader = new QueueLoader();
			
			_loader.addItem( imageUrl, _alvo, { title:"image" } );
			
			percentTxt.text = '0%';
			
			/*
			_loader.addEventListener( QueueLoaderEvent.ITEM_COMPLETE, 	onLoadItemComplete, 	false, 0, true );
			_loader.addEventListener( QueueLoaderEvent.QUEUE_START, 	onLoadQueueStart, 		false, 0, true );
			*/
			
			_loader.addEventListener( QueueLoaderEvent.QUEUE_PROGRESS, 	onLoadQueueProgress, 	false, 0, true );
			_loader.addEventListener( QueueLoaderEvent.QUEUE_COMPLETE, 	onLoadQueueComplete, 	false, 0, true );
			
			_loader.execute();
			
		}		
		
	}
	
}